# -*- coding: utf-8 -*-

import requests,json,os,urllib,string,sys
import scrapy

reload(sys)
sys.setdefaultencoding('utf8')
sys.path.insert(0, '/phhui/work/python/spider')
from sqlHelper import *

class Win007Spider(scrapy.Spider):
    name = 'win007'
    allowed_domains = ['win007.com']
    handle_httpstatus_list = [301,403,404,442,504]
    offset=60
    url='http://zq.win007.com/jsData/teamInfo/teamDetail/tdl'
    endurl='.js?version=201804042515'
    start_urls = [url+str(offset)+endurl]
    def parse(self, res):
        print json.dumps(response.header)
        if response.status==404 or response.status==301 or response.status==403 or response.status==442 or response.status==504:
            print self.url+str(self.offset)+'------页面不存在'+str(response.status)
        else:
            ct=response.body
            ct=ct[22:]
            cn=ct[0:ct.index(',')]
            ct=ct[ct.index(',')+2:]
            big=ct[0:ct.index(',')]
            ct=ct[ct.index(',')+2:]
            en=ct[0:ct.index(',')]
            ct=ct[ct.index(',')+2:]
            url=ct[0:ct.index(',')]

            print cn+'--'+big+'--'+en+'--'+url
            return
            if self.save_data(en,cn,big):
                self.save_img(url,en)

        if self.offset < 90623:
            self.offset += 1
        yield scrapy.Request(self.url+str(self.offset)+self.endurl, callback = self.parse,dont_filter=True)

    def save_data(self,en,cn,big):
        if sqlHelper.selectOne("select id from team where cn=%s and type=%S",(cn,30)):
            if sqlHelper.update("UPDATE team SET source=%s,en=%s WHERE cn=(SELECT cn FROM (SELECT * FROM team WHERE en='' and cn=%s and typeText=%s) AS t)",(self.url+str(self.offset),en,cn,'足球')):
                print '更新'+cn+'--en>>'+en
            if sqlHelper.update("UPDATE team SET source=%s,big=%s WHERE cn=(SELECT cn FROM (SELECT * FROM team WHERE big='' and cn=%s and typeText=%s) AS t)",(self.url+str(self.offset),big,cn,'足球')):
                print '更新'+cn+'--big>>'+big
            if sqlHelper.update("UPDATE team SET source=%s,logo=%s WHERE cn=(SELECT cn FROM (SELECT * FROM team WHERE logo='' and cn=%s and typeText=%s) AS t)",(self.url+str(self.offset),logo,cn,'足球'    )):
                print '更新'+cn+'--logo>>'+logo
            return False

        else:
            sqlHelper.update("insert into team(cn,en,logo,big,type,typeText,source) values(%s,%s,%s,%s,%s,%s,source)",(cn,en,en,big,30,'足球',self.url+str(self.offset)+self.endurl))
        return True
    
    def save_img(self,img_url,file_name,file_path='img'):
        img_url=img_url.replace('\/','/')
        try:
            if not os.path.exists(file_path):
                print '文件夹',file_path,'不存在，重新建立'
                #os.mkdir(file_path)
                os.makedirs(file_path)
            #获得图片后缀
            file_suffix = os.path.splitext(img_url)[1]
            #拼接图片名（包含路径）
            filename = '{}{}{}{}'.format(file_path,os.sep,file_name,file_suffix)
            filename=filename.replace('.com','_com')
            if string.find(filename,'.')==-1:
                filename=filename+'.png'
            if string.find(filename,'?')!=-1:
                filename=filename[0:len(filename)-4]
#       m下载图片，并保存到文件夹中
            urllib.urlretrieve(img_url,filename=filename)
        except IOError as e:
            print '文件操作失败',e
        except Exception as e:
            print '错误 ：',e

